<template>
  <div>
    <el-card style="margin: 20px 0">
      <CategorySelect @getCategoryId="getCategoryId" :show="scene != 'spuList'"></CategorySelect>
    </el-card>
    <el-card>
      <!-- 这里将来有三部分进行切换 -->
      <div v-show="scene == 'spuList'">
        <!-- 展示spu列表的结构 -->
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          :disabled="!category3Id"
          @click="addSpu"
        >添加SPU</el-button>
        <el-table style="width: 100%" border :data="records">
          <el-table-column type="index" label="序号" width="95" align="center"></el-table-column>
          <el-table-column prop="spuName" label="spu名称" width="width"></el-table-column>
          <el-table-column prop="description" label="spu描述" width="width"></el-table-column>
          <el-table-column prop="prop" label="操作" width="width">
            <template slot-scope="{row}">
              <el-button
                type="success"
                icon="el-icon-plus"
                size="mini"
                title="添加sku"
                @click="addSku(row)"
              ></el-button>
              <el-button
                type="warning"
                icon="el-icon-edit"
                size="mini"
                title="修改spu"
                @click="updateSpu(row)"
              ></el-button>
              <el-button
                type="info"
                icon="el-icon-info"
                size="mini"
                title="查看spu的全部sku"
                style="margin-right:10px"
                @click="openSkuList(row)"
              ></el-button>
              <el-popconfirm :title="`确定删除${row.spuName}吗？`" @onConfirm="deleteSpu(row)">
                <el-button
                  type="danger"
                  icon="el-icon-delete"
                  size="mini"
                  title="删除spu"
                  slot="reference"
                ></el-button>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页器 -->
        <el-pagination
          style="text-align: center"
          :current-page="page"
          :page-sizes="[3, 5, 10]"
          :page-size="limit"
          layout="prev, pager, next, jumper,->,sizes, total"
          :total="total"
          @current-change="handleCurrentChange"
          @size-change="handleSizeChange"
        ></el-pagination>
      </div>
      <SpuForm v-show="scene == 'addSpuOrEdtSpu'" @changeScene="changeScene" ref="spu"></SpuForm>
      <SkuForm v-show="scene == 'addSku'" ref="sku" @change_Scene="change_Scene"></SkuForm>
    </el-card>
    <!-- dialog对话框 -->
    <el-dialog :title="`${spu.spuName}的sku列表`" :visible.sync="dialogTableVisible" :before-close="close">
      <el-table :data="skuList" border  v-loading="loading">
        <el-table-column property="skuName" label="名称" width="width"></el-table-column>
        <el-table-column property="price" label="价格" width="width"></el-table-column>
        <el-table-column property="weight" label="重量" width="width"></el-table-column>
        <el-table-column label="默认图片" width="width">
          <template slot-scope="{row}">
            <el-image style="width: 100px; height: 100px" :src="row.skuDefaultImg"></el-image>
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
  </div>
</template>

<script>
import SpuForm from './SpuForm';
import SkuForm from './SkuForm';
export default {
  name: 'Spu',
  components: {
    SpuForm,
    SkuForm,
  },
  data () {
    return {
      show: true,
      category1Id: '',
      category2Id: '',
      category3Id: '',
      page: 1, //初始化分页器当前第几页
      limit: 3, //每一页需要展示多少条数据
      records: [], //存储spu列表的数据
      total: 0, //分页器一共需要展示数据的条数
      scene: 'spuList', //控制显示spu列表还是修改spu的界面切换的字段
      dialogTableVisible: false, //控制dialog对话框的显示与隐藏的
      spu: {}, //点击查看spu的所有的sku时事件传过来的spu,存储下来
      skuList: [], //存储从服务器请求到的sku列表的数据
      loading: true, //dialog窗口在数据还没来前显示loading效果
    }
  },
  methods: {
    async getSpuList () {
      // 携带三个参数
      const { page, limit, category3Id } = this;
      let result = await this.$API.spu.reqSpuList(page, limit, category3Id)
      // console.log(result);
      if (result.code == 200) {
        this.total = result.data.total;
        this.records = result.data.records;
      }
    },
    getCategoryId ({ category1Id, category2Id, category3Id }) {
      this.category1Id = category1Id;
      this.category2Id = category2Id;
      this.category3Id = category3Id;
      // 获取spu列表数据进行展示
      this.getSpuList();
    },
    // 点击分页器第几页按钮的回调
    handleCurrentChange (page) {
      // 修改参数
      this.page = page;
      // 再次发送请求
      this.getSpuList()
    },
    // 改变每页展示数据的条数的回调
    handleSizeChange (limit) {
      this.limit = limit;
      this.getSpuList();
    },
    // 点击添加spu的回调
    addSpu () {
      this.scene = 'addSpuOrEdtSpu'
      // 通知子组件发送请求获取默认的品牌和商品属性
      this.$refs.spu.addSpuData(this.category3Id)
    },
    // 点击修改spu按钮的回调
    updateSpu (row) {
      this.scene = 'addSpuOrEdtSpu';
      // 获取子组件
      this.$refs.spu.initSpuData(row)
    },
    // 点击SpuForm组件中的取消按钮 页面切回到spuList
    changeScene () {
      this.scene = 'spuList';
      // 重新请求数据再次渲染
      this.getSpuList(this.page)
    },
    // 点击SkuForm组件中的取消按钮 页面切回到spuList
    change_Scene () {
      this.scene = 'spuList';
    },
    // 删除spu
    async deleteSpu (row) {
      let result = await this.$API.spu.reqDeleteSpu(row.id)
      if (result.code == 200) {
        this.$message({ type: 'success', message: '删除成功' })
        // 再次请求数据
        this.getSpuList(this.records.length > 1 ? this.page : this.page - 1);
      }
    },
    // 点击添加sku的回调
    addSku (row) {
      // 切换到skuForm页面
      this.scene = 'addSku';
      // 父组件调用子组件的方法，让子组件发送请求----三个请求
      this.$refs.sku.getData(this.category1Id, this.category2Id, this.category3Id, row);

    },
    // 查看spu的全部sku按钮的回调
    async openSkuList (spu) {
      // 存储spu
      this.spu = spu;
      // console.log(spu);
      this.dialogTableVisible = true;
      // 获取sku列表的数据进行展示
      let result = await this.$API.spu.reqSkuList(spu.id);
      console.log(result);
      if (result.code == 200) {
        this.skuList = result.data;
        // 隐藏Loading
        this.loading = false;
      }
    },
    // 关闭dialog的回调
    close(done){
      // 将loading的值变为true
      this.loading = true;
      // 清空skuList的数据
      this.skuList = [];
      // 关闭对话框
      done()
    }



  }
}
</script>

<style lang="less" scoped>
</style>